package CI;
import java.util.ArrayList;

public class Parser {
	public static ArrayList<Object> parse(String s) throws Exception {
		return parseTokens(Tokenizer.tokenize(s), false);
	}
	private static ArrayList<Object> parseTokens(ArrayList<String> tokens, boolean inner) throws Exception {
		ArrayList<Object> res = new ArrayList<Object>();		
		while (tokens.size() > 0) {
			String current = tokens.get(0);
			tokens.remove(0);
			if (current.equals("(")) 
				res.add(parseTokens(tokens,true));
			else if (current.equals(")")) {
				if (inner) 
					return res;
				else {
					throw new ParseError("Unmatched close paren");
				}
			}
			else {
				res.add(current);
			}
		}
		if (inner) {
			throw new ParseError("Unmatched open paren");
		}
		else {	
			return res;
		}
	}
}
